<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { login } from '@/api/System.js';
import router from '@/router/index.ts';
import { useAuthStore } from '@/stores/auth'; // 引入 Pinia 仓库

interface LoginForm {
  username: string;
  password: string;
}

const authStore = useAuthStore(); // 初始化 Pinia 仓库
const loginFormRef = ref(null);
const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
};

const loginSystem = (data: LoginForm) => {
  login(data).then((res:any) => {
    if (res.code === 200) {
      // 登录成功，保存 sessionId 到 Pinia 和 localStorage
      const sessionId = res.data.sessionId; // 假设接口返回的数据结构包含 sessionId
      authStore.setSession(sessionId); // 更新 Pinia 状态
      localStorage.setItem('JSESSIONID', sessionId); // 持久化保存到 localStorage
      ElMessage.success('登录成功！');
      // 确保跳转
      router.replace('/main')
      .then(() => {
        window.location.reload() // 强制刷新页面
      })
    } else {
      // 登录失败，显示错误提示
      ElMessage.error(`登录失败：${res.message || '未知错误'}`);
    }
  }).catch(() => {
    // 处理请求异常
    ElMessage.error('网络错误，请稍后重试');
  });
};

const handleLogin = () => {
  if (!loginFormRef.value) return;
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loginSystem(loginForm);
    } else {
      ElMessage.error('验证失败！');
      return false;
    }
  });
};
</script>
<template>
  <div class="login-container">
    <!-- 左侧插图 -->
    <div class="left-side">
      <!-- <img src="@/assets/images/one.svg" alt="员工自助盘点插图" class="illustration" /> -->
      <!-- <div class="description">
        <h2>资产盘点</h2>
        <p>高效盘点，云端审核，漏盘分析，让资产盘点轻松无忧！</p>
      </div> -->
    </div>

    <!-- 右侧登录区域 -->
    <div class="right-side">
      <div class="login-box">

        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form glassy-card">
          <el-form-item>
            <div class="login-title">登录</div>
          </el-form-item>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入登录账号/手机号码" class="glassy-input" clearable/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" class="glassy-input"
                      clearable @keydown.enter="handleLogin"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin" class="glassy-button">登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background: url('@/assets/images/bg.jpg') center center;
  /* 背景图片覆盖整个容器 */
  background-size: cover;
  /* background: linear-gradient(135deg, #e6f7ff, #f9f9f9); */
}

.left-side {
  width: 50%;
  /* background-color: #e6f7ff; */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.illustration {
  width: 70%;
  transition: transform 0.3s ease;
}

.illustration:hover {
  transform: scale(1.05);
}

.description {
  text-align: center;
  margin-top: 20px;
  color: #555;
  color: #fff;

}

.right-side {
  width: 50%;
  /* background: linear-gradient(135deg, rgba(255, 255, 255, 0.3), rgba(240, 240, 240, 0.2)); */
  /* backdrop-filter: blur(20px); */
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  width: 100%;
  max-width: 520px;
  /* height: 400px; */
  padding: 40px;
  box-sizing: border-box;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  color: #fff;

  /* margin-bottom: 40px; */
  text-align: center;
}

.login-form {
  width: 100%;
  height: 280px;

}

/* 毛玻璃卡片 */
.glassy-card {
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(15px);
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  padding: 48px 34px 34px 34px;
  transition: box-shadow 0.3s ease;
  transition: all 0.3s ease;
}

.glassy-card:hover {
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.1);
  transform: scale(1.08);
  transition: transform 0.3s ease;
}


/* 液态按钮 */
.glassy-button {
  width: 100%;
  background: linear-gradient(135deg, #00c8a0, #00d4b1);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  padding: 12px 0;
  color: white;
  box-shadow: 0 4px 16px rgba(0, 192, 160, 0.3);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.glassy-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 192, 160, 0.4);
}

/deep/ .el-form-item__content .el-button {
  margin-top: 20px !important;
}
</style>